package oss

import (
	"context"
	"time"

	"github.com/lab-online/pkg/oss"
)

type OSS struct {
	oss.OSSManager
}

func New(ossManager oss.OSSManager) *OSS {
	return &OSS{ossManager}
}

func (o *OSS) PresignedPutObjectURL(ctx context.Context, bucketName string, objectKey string, expire time.Duration) (string, error) {
	bucket, err := o.NewBucket(bucketName, "", false)
	if err != nil {
		return "", err
	}

	url, err := bucket.PresignedPutObject(ctx, objectKey, expire)
	if err != nil {
		return "", err
	}

	// if config, err := bucket.GetBucketNotification(ctx); err != nil {
	// 	return "", err
	// } else if !slices.ContainsFunc(config.QueueConfigs, func(conf notification.QueueConfig) bool {
	// 	return conf.Arn.Resource == bucketName
	// }) {
	// 	config.AddQueue(notification.
	// 		NewConfig(notification.Arn{Service: "s3", Resource: bucketName}))
	// 	// config.a

	// 	if err := bucket.SetBucketNotification(ctx, config); err != nil {
	// 		logger.Error("set configured", err)
	// 		return "", err
	// 	}
	// }

	return url.String(), nil
}

func (o *OSS) PresignedGetObjectURL(bucketName string, objectKey string, expireTime int64) (string, error) {
	bucket, err := o.NewBucket(bucketName, "", false)

	if err != nil {
		return "", err
	}

	url, err := bucket.PresignedGetObject(context.TODO(), objectKey, expireTime, nil)

	if err != nil {
		return "", err
	}

	return url.String(), nil
}
